angular.module('tab-market', ['flow-div', 'slide-banner'])

.controller 'TabMarketCtrl', ($localStorage,$location, $scope, $state, $http,$stateParams, $ionicHistory, $ionicViewSwitcher,$ionicPopup,paginationFactory,userData,showToast,$ionicScrollDelegate,$ionicSlideBoxDelegate) ->
    $scope.$storage = $localStorage
    getNoread = ->
        $http.get('/updshop/',params:{action:'noreadcount',userid:userData.getToken()})
        .success (resp) ->
            if resp?.status == '0'
                $scope.noRead = resp.procount
            else
                showToast resp.error
        .error ->
            showToast '网络异常,请检查网络是否连接'
    paginationSrv = new paginationFactory('TabMarketCtrl','/updshop/')
    unWatch = paginationSrv.watch (data) ->
        $scope.$storage.tabMarketGoodList = data
    $http.get('/updshop/',params:{action:'productclass'})
    .success (resp) ->
        if resp?.status == '0'
            $scope.classes = resp.classlist
            $scope.items = [
                {id:0,name:'全部',func: $scope.goodsInClass}
            ]
            _.each $scope.classes,(item)->
                $scope.items.push {id:item.classid,name:item.classname, func: $scope.goodsInClass}
            paginationSrv.reload(true, {
                action:'productlist'
                sortid:$scope.items[0].id
                stype:0
                userid:userData.getToken()
            })
        else
            showToast resp.error
    .error ->
        showToast '网络异常,请检查网络是否连接'

    $scope.sorry = ->
        $ionicViewSwitcher.nextDirection('forward')
        showToast '该功能暂未开放'
    $scope.goodsInClass = (i) ->
        paginationSrv.reload(true, {
            action:'productlist'
            sortid:$scope.items[i].id
            stype:0
            userid:userData.getToken()
        })
    $scope.pick = () ->
        console.log $localStorage['goodItem']
        $localStorage['goodMarketDetail'] = undefined
        $localStorage['banners'] = undefined
        $localStorage['images'] = undefined
        getMarketDetail()
        $state.go 'tab.market-detail'
    getMarketDetail = ->
        $http.get('/updshop/',params:{action:'productinfo',id:$localStorage['goodItem'].id,isupk:'1',userid:userData.getToken()})
        .success (resp) ->
            if resp?.status == '0'
                $localStorage['goodMarketDetail'] = resp
                $localStorage['banners'] = resp.imgs
                $localStorage['images'] = resp.images
            else
                showToast '没有该商品'
        .error ->
            showToast '网络异常,请检查网络是否连接'
    $http.get('/advertise/',params:{action:'updshop'})
    .success (resp) ->
        if resp?.status == '0'
            $scope.banners = resp.body
            $ionicSlideBoxDelegate.$getByHandle('banner').update()
            $ionicSlideBoxDelegate.$getByHandle('banner').loop(true)
            console.log $scope.banners
        else
            showToast resp.error
    .error ->
        showToast '网络异常,请检查网络是否连接'

    getReferencepro = ->
        $http.get('/updshop/', params: {action: 'referencepro', userid: userData.getToken()})
        .success (resp) ->
            if resp?.status == '0'
                $localStorage['newGoodsReferencepro'] = resp.body
            else
                showToast resp.error
        .error ->
            showToast '网络异常,请检查网络是否连接'

    getNewProduct =->
        $http.get('/updshop/',params:{action:'newproduct',userid:userData.getToken()})
        .success (resp) ->
            if resp?.status == '0'
                $localStorage['newGoodsNewProduct'] = resp.body
            else
                showToast resp.error
        .error ->
            showToast '网络异常,请检查网络是否连接'
    _.extend $scope,
        canLoadMore: -> paginationSrv.canLoadMore()
        doRefresh: ->
            paginationSrv.reload(true).finally -> $scope.$broadcast 'scroll.refreshComplete'
        loadMore: ->
            paginationSrv.loadMore().finally -> $scope.$broadcast 'scroll.infiniteScrollComplete'
        goNewproduct: ->
            getNewProduct()
            $state.go 'tab.market-newproduct'
        goReferencepro: ->
            getReferencepro()
            $state.go 'tab.market-referencepro'
        goReferpro: ->
            $state.go 'tab.referencepro'
        goSearch:->
            $state.go 'tab.goods-search-home',currentItemId:'10'
        goBack: ->
            $ionicViewSwitcher.nextDirection('back')
            $ionicHistory.goBack()


    $scope.$on '$destory', ->
        unWatch()
    $scope.$on '$ionicView.enter', ->
        getNoread()
        console.log $stateParams.origin



